package com.zhenyi.jiahua.core.utils;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.jfinal.kit.JsonKit;
import com.jfinal.kit.StrKit;
import com.jiahua.common.model.JhChannel;
import com.jiahua.common.model.JhStaff;
import com.jiahua.common.model.JhStudent;

import cn.hutool.core.io.file.FileWriter;
import cn.hutool.core.lang.Console;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.sax.handler.RowHandler;

public class ImportUtils {

	public void run() {
		// String path = "C:\\Users\\Administrator\\Documents\\WeChat
		// Files\\qq350909686\\Files\\1013-资源数据-15年全部资源.xlsx";
		String path = "/Users/cen/Desktop/0导入模板.xlsx";
		System.out.println("start");
		ExcelReader reader = ExcelUtil.getReader(path);
		List<List<Object>> list = reader.read();
		for (int index = 0; index < list.size(); index++) {
			System.out.println("go");
			if (index <= 1) {
				continue;
			}
			List<Object> row = list.get(index);
			System.out.println(JsonKit.toJson(row));
			System.out.println("done");
			return;
		}
	}

	// public static void main(String[] args) {
	// new ImportUtils().go();
	// }

	public void go() {
		String path = "/Users/cen/Desktop/0导入模板.xlsx";
		System.out.println("start");
		ExcelUtil.read07BySax(path, 0, handler);
	}

	private FileWriter writer = new FileWriter("/Users/cen/Desktop/jh/log.txt");
	private FileWriter writerS = new FileWriter("/Users/cen/Desktop/jh/suc.txt");

	private void doError() {
		String[] ab = {};
		System.out.println(ab[3]);
	}

	private Date getDate(String dt) {
		if (StrKit.isBlank(dt)) {
			return null;
		}
		try {
			Date date = getDate(dt, "yyyy-MM-dd");
			return date;
		} catch (Exception e) {
		}
		log("date error " + dt);
		System.out.println("date error " + dt);
		return null;
	}

	private Date getDate2(String dt) {
		if (StrKit.isBlank(dt)) {
			return null;
		}
		try {
			getDate(dt, "yyyy-MM-dd");
			dt = dt + " 13:00";
			try {
				Date date2 = getDate(dt, "yyyy-MM-dd HH:mm");
				return date2;
			} catch (Exception e) {
			}
		} catch (Exception e) {
		}
		try {
			Date date = getDate(dt, "yyyy-MM-dd HH:mm");
			return date;
		} catch (Exception e) {
		}
		log("date error " + dt);
		System.out.println("date error " + dt);
		return null;
	}

	private Date getDate(String dt, String fmt) {
		SimpleDateFormat df2 = new SimpleDateFormat(fmt);
		try {
			return df2.parse(dt);
		} catch (ParseException e) {
		}
		return null;
	}

	private String getChannelIds(String channel) {
		if (StrKit.isBlank(channel)) {
			return null;
		}
		String[] channels = channel.split("/");
		String ids = "";
		for (String string : channels) {
			JhChannel jhChannel = JhChannel.dao.searchFirst("name", string);
			if (jhChannel != null) {
				ids = ids + jhChannel.getId() + ",";
			} else {
				System.out.println("channel error " + channel + " " + string);
				return ids;
			}
		}
		return ids;
	}

	private Long getStaff(String name) {
		try {
			if (StrKit.isBlank(name)) {
				return null;
			}
			JhStaff staff = JhStaff.dao.searchFirst("name", name);
			if (staff != null) {
				return staff.getId();
			} else {
				return null;
			}
		} catch (Exception e) {
			return null;
		}
	}

	private int newCount = 0;

	private void log(String text) {
		// System.out.println(text);
		writerS.append(text + "\n");
	}

	RowHandler handler = new RowHandler() {

		@Override
		public void handle(int sheetIndex, int rowIndex, List<Object> rowlist) {

			if (rowIndex % 500 == 0) {
				System.out.println("进度 - " + rowIndex);
			}
			if (rowIndex <= 0) {
				return;
			}
			String rowStr = JsonKit.toJson(rowlist);
			// try {
			log("===========原始数据");
			log(rowStr);
			Console.log("[{}] [{}] {}", sheetIndex, rowIndex, rowlist);
			String schoolName = (String) rowlist.get(0);
			Long schoolId = null;
			if ("九洲城校区".equals(schoolName)) {
				schoolId = 1L;
			} else if ("华发校区".equals(schoolName)) {
				schoolId = 2L;
			} else {
				log("error:" + schoolName);
				// doError();
				return;
			}

			/**
			 * 表格为准
			 */
			String name = (String) rowlist.get(1);
			String mobile = String.valueOf(rowlist.get(2));
			if (StrKit.isBlank(mobile)) {
				log("mobile error");
				return;
			}
			JhStudent student = JhStudent.dao.searchFirst("mobile", mobile);
			if (student == null) {
				log("save student " + ++newCount);
				// student = new JhStudent();
				// student.setName(name);
				// student.setMobile(mobile);
				// student.setSchoolId(schoolId);
				// student.save();
				// // return;
			} else {
				log(JsonKit.toJson(student));
			}
			//
			// if (schoolId != null) {
			// log("schoolId: " + schoolId);
			// student.setSchoolId(schoolId);
			// }
			// log(mobile + " " + name);
			// // student.print();
			//
			// if (StrKit.notBlank(name)) {
			// log("name: " + name);
			// student.setName(name);
			// }
			//
			// Integer status =
			// Integer.parseInt(String.valueOf(rowlist.get(4)));
			// student.setType(status);
			// log("type: " + status);
			//
			// String level = String.valueOf(rowlist.get(5));
			// if (StrKit.notBlank(level)) {
			// log("等级: " + level);
			// student.setTag(level);
			// }
			//
			// String contract = (String) rowlist.get(6);
			// if (StrKit.notBlank(contract)) {
			// log("contract: " + contract);
			// student.setContract(contract);
			// }
			//
			// String price = (String) rowlist.get(14);
			// if (StrKit.notBlank(price)) {
			// log("price: " + price);
			// student.setCourseFee(price);
			// }
			// // String price = (String) rowlist.get(6);
			//
			// Date contractStart = getDate((String) rowlist.get(11));
			// Date contractEnd = getDate((String) rowlist.get(12));
			// if (contractStart != null && contractEnd != null) {
			// log("contract: " + contractStart + " - " + contractEnd);
			// student.setContractStartDate(contractStart);
			// student.setContractEndDate(contractEnd);
			// }

			/**
			 * 进1
			 */
			String coreCount = (String) rowlist.get(15);
			if (StrKit.notBlank(coreCount)) {
				BigDecimal bd = new BigDecimal(coreCount).setScale(0, BigDecimal.ROUND_HALF_UP);
				int count = Integer.parseInt(bd.toString());
				log("核心课数:" + count + " - " + coreCount);
				student.setCoreCourseLeft(count);
			}

			// /**
			// * 系统为准
			// */
			// String english = (String) rowlist.get(16);
			// String sysEnglish = student.getEnglishName();
			// if (StrKit.isBlank(sysEnglish)) {
			// if (StrKit.notBlank(english)) {
			// log("english: " + english);
			// student.setEnglishName(english);
			// }
			// }
			//
			// String sex = (String) rowlist.get(17);
			// Integer sexNum = null;
			// if ("男".equals(sex)) {
			// sexNum = 1;
			// } else if ("女".equals(sex)) {
			// sexNum = 2;
			// }
			// if (sexNum != null) {
			// log("sex: " + sexNum);
			// student.setSex(sexNum);
			// }
			//
			// String channel = (String) rowlist.get(18);
			// log("去到:" + channel);
			// String channels = getChannelIds(channel);
			// log("渠道:" + channels);
			// student.setChannel(channels);
			// student.setChannelStr(channel.replaceAll("/", ","));
			//
			// Long tmkId = getStaff(String.valueOf(rowlist.get(19)));
			// if (tmkId != null) {
			// log("tmkId: " + tmkId);
			// student.setTmkId(tmkId);
			// }
			//
			// Long tuiguangId = getStaff(String.valueOf(rowlist.get(20))); //
			// rowlist.get(20);
			// if (tuiguangId != null) {
			// log("tuiguangId: " + tuiguangId);
			// student.setTuiguangId(tuiguangId);
			// }
			//
			// Date addTime = getDate2(String.valueOf(rowlist.get(21)));
			// if (addTime != null) {
			// log("addTime: " + addTime);
			// student.setAddTime(addTime);
			// }
			// // getStaff(String.valueOf(rowlist.get(21)));(String);//
			// // rowlist.get(21);
			//
			// Long ccId = getStaff(String.valueOf(rowlist.get(23))); //
			// rowlist.get(23);
			// if (ccId != null) {
			// log("ccId: " + ccId);
			// student.setCcId(ccId);
			// }
			// Long taId = getStaff(String.valueOf(rowlist.get(24))); //
			// rowlist.get(24);
			// if (taId != null) {
			// log("taId: " + taId);
			// student.setTaId(taId);
			// }
			//
			// String mark = (String) rowlist.get(22);
			// if (StrKit.notBlank(mark)) {
			// log("mark: " + mark);
			// if (StrKit.isBlank(student.getMark())) {
			// student.setMark(mark);
			// } else {
			// student.setMark(student.getMark() + mark);
			// }
			// }
			//
			// String lt = (String) rowlist.get(25);
			// if (StrKit.notBlank(lt)) {
			// log("lt: " + lt);
			// student.setLt(lt);
			// }
			// String demo = (String) rowlist.get(26);
			// if (StrKit.notBlank(demo)) {
			// log("demo: " + demo);
			// student.setDemoTeacher(demo);
			// }
			// String degree = (String) rowlist.get(27);
			// if (StrKit.notBlank(degree)) {
			// log("degree" + degree);
			// student.setDegree(degree);
			// }
			//
			// String followType = (String) rowlist.get(28);
			// if (StrKit.notBlank(followType)) {
			// log("followType: " + followType);
			// student.setFollowType(followType + "类");
			// }
			student.update();
			log("update student");
			log(JsonKit.toJson(student));
			log("=====================\n");

			// if (rowIndex > 6) {
			// System.out.println("end");
			// doError();
			// }

			// } catch (Exception e) {
			// // e.printStackTrace();
			// writer.append("error: " + rowIndex + "\n");
			// writer.append(rowStr + "\n");
			// System.out.println("error: " + rowIndex);
			// System.out.println(rowStr);
			// }

		}
	};

	public RowHandler createRowHandler() {
		return new RowHandler() {
			@Override
			public void handle(int sheetIndex, int rowIndex, List<Object> rowlist) {
			}
		};
	}
}
